<%@ page language="java" pageEncoding="utf-8"%>
<div class="box">
	<div class="cell">社区讨论</div>
	<div class="inner">
	    <%
	    if(session.getAttribute("uid")!=null){
	    	%>
            <div class="form-group has-success">
              <input type="text" class="form-control" id="msg" ><button  id="sub" class="btn btn-primary" onclick="sendMsg()">发送</button>
            </div>         
            <div id="conDiv">
		    </div>
	    	<%
	    }else{
	    	%>
	    	<p>在线公告板，登陆后可以在线讨论！</p>
	    	<%
	    }
	    %>
	    
	</div>
	<script type="text/javascript">
	function sendMsg() {
		var mm=$("#msg").val();
		if(mm==""){
			alert("请输入内容！");
			return;
		}
	    var jsonData = {
	        'msg': $("#msg").val(),
	        'uid': $("#userId").val()
	    };
	    $.ajax({
	        url: "message/sendMessage",
	        data: jsonData,
	        type: "post",
	        success: function (backdata) {
	            if (backdata == 1) {
	                //resetFr();
	                $("#msg").val("");
	                getMsg();
	            } else if (backdata == 0) {
	                alert("发送失败");
	            } else {
	                //alert("防止数据不断增长，会影响速度，请先删掉一些数据再做测试");
	            }
	        }, error: function (error) {
	            console.log(error);
	        }
	    });
	}
	
	function resetFr() {
	    $('form').each(function (index) {
	        $('form')[index].reset();
	    });
	}
	
	function getMsg(){
		$.ajax({
	        url: "message/getMessage",
	        data: "",
	        type: "post",
	        success: function (backdata) {
	            if (backdata !=null) {
	            	$("#conDiv").empty();
	                var obj = eval(backdata);
	                var str="";
	                $(obj).each(function(index) {
	                    var val = obj[index];
	                    str = str+ "<ul class=\"list-group\"><li class=\"list-group-item list-group-item-info\"><span class=\"badge\">"+val.tim+"分钟前</span><h6 class=\"list-group-item-heading\">"+val.uname+"</h6><p class=\"list-group-item-text\">"+val.message+"</p></li></ul>";
	                });
	                $("#conDiv").append(str);
	            } 
	        }, error: function (error) {
	            console.log(error);
	        }
	    });
	}
	
	setInterval('getMsg()',1000);
	</script>
</div>